//
//  Lawyering.swift
//  UUVideo
//
//  Created by Galaxy on 2023/5/4.
//  Copyright © 2023 qykj. All rights reserved.
//

import UIKit
import Ji
class Lawyering: WebsiteBaseModel {
    override init() {
        super.init()
        websiteName = "先锋影音"
        webUrlStr = "https://www.007xf.com/"
        valueArr = ["6", "7", "3", "4", "22"]
    }
    
    override func getIndexData() -> [ListModel] {
        let jiDoc = Ji.init(htmlURL: URL.init(string: webUrlStr)!)
        if jiDoc == nil {
            return []
        }
        let divArr = [2, 3, 6, 7, 8]
        let titleArr = ["一本道", "加勒比", "中文", "东热","有码"]
        var resultArr: [ListModel] = []
        for (index, item) in divArr.enumerated() {
            let listModel = ListModel.init()
            let titleXpath = "//div[@class='myui-panel myui-panel-bg clearfix'][\(item)]/div/div/ul[1]/li/div/a/@title"
            let urlXpath = "//div[@class='myui-panel myui-panel-bg clearfix'][\(item)]/div/div/ul[1]/li/div/a/@href"
            let imgXpath = "//div[@class='myui-panel myui-panel-bg clearfix'][\(item)]/div/div/ul[1]/li/div/a/@data-original"
            let titleNodeArr = jiDoc?.xPath(titleXpath)
            let urlNodeArr = jiDoc?.xPath(urlXpath)
            let imgNodeArr = jiDoc?.xPath(imgXpath)
            listModel.title = titleArr[index]
            listModel.more = true
            listModel.list = []
            for (i, _) in titleNodeArr!.enumerated() {
                var videoModel = VideoModel.init()
                videoModel.name = titleNodeArr![i].content!
                videoModel.webType = websiteType.lawyering.rawValue
                let detailUrl: String = urlNodeArr![i].content!
                videoModel.detailUrl = Tool.checkUrl(urlStr: detailUrl, domainUrlStr: webUrlStr)
                let picUrl: String = imgNodeArr![i].content!
                videoModel.picUrl = Tool.checkUrl(urlStr: picUrl, domainUrlStr: webUrlStr)
                videoModel.num = ""
                videoModel.type = 3
                listModel.list.append(videoModel)
            }
            resultArr.append(listModel)
        }
        return resultArr
    }
    override func getVideoList(videoTypeIndex: Int, category: (area: String, year: String, videoCategory: String), pageNum: Int) -> [ListModel] {
        let videoType = valueArr[videoTypeIndex]
        let urlStr = webUrlStr + "XfDy/\(videoType)-\(pageNum).html"
        let newUrlStr = urlStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        let jiDoc = Ji(htmlURL: URL.init(string: newUrlStr)!)
        if jiDoc == nil {
            return []
        }
        let listModel = ListModel.init()
        listModel.title = ""
        listModel.more = false
        listModel.list = []
        let titleXpath = "//div[@class='myui-panel active myui-panel-bg clearfix']/div/div/ul[1]/li/div/a/@title"
        let urlXpath = "//div[@class='myui-panel active myui-panel-bg clearfix']/div/div/ul[1]/li/div/a/@href"
        let imgXpath = "//div[@class='myui-panel active myui-panel-bg clearfix']/div/div/ul[1]/li/div/a/@data-original"
        let titleNodeArr = jiDoc?.xPath(titleXpath)
        let urlNodeArr = jiDoc?.xPath(urlXpath)
        let imgNodeArr = jiDoc?.xPath(imgXpath)
        for (index,item) in titleNodeArr!.enumerated() {
            var videoModel = VideoModel.init()
            videoModel.name = titleNodeArr![index].content!
            videoModel.num = ""
            let detailUrl: String = urlNodeArr![index].content!
            videoModel.detailUrl = Tool.checkUrl(urlStr: detailUrl, domainUrlStr: webUrlStr)
            let picUrl: String = imgNodeArr![index].content!
            videoModel.picUrl = Tool.checkUrl(urlStr: picUrl, domainUrlStr: webUrlStr)
            videoModel.type = 3
            videoModel.webType = websiteType.lawyering.rawValue
            listModel.list.append(videoModel)
        }
        return [listModel]
    }
    override func getVideoCategory(videoTypeIndex: Int) -> [CategoryListModel] {
        []
    }
    
    override func getVideoDetail(urlStr: String) -> (result: Bool, model: VideoModel) {
        let jiDoc = Ji(htmlURL: URL.init(string: urlStr)!)
        if jiDoc == nil {
            return (result: false, model: VideoModel.init())
        }
        let baseUrl = Tool.getRegularData(regularExpress: "((http://)|(https://))[^\\.]*\\.(?<domain>[^/|?]*)", content: urlStr)[0]
        var videoModel = VideoModel.init()
        videoModel.detailUrl = urlStr
        videoModel.videoArr = []
        videoModel.tagArr = []
        videoModel.serialArr = []
        //        剧集
        // 获取线路
        let model = CircuitModel.init()
        model.name = "默认线路"
        let serialPathXpath = "//div[@class='tab-content myui-panel_bd']/div/ul/li/a/@href"
        let serialtitleXpath = "//div[@class='tab-content myui-panel_bd']/div/ul/li/a"
        let serialUrlNodeArr = jiDoc?.xPath(serialPathXpath)
        let serialTitleNodeArr = jiDoc?.xPath(serialtitleXpath)
        if serialUrlNodeArr!.count > 0 {
            for (index, item) in serialUrlNodeArr!.enumerated() {
                let serial = SerialModel.init()
                serial.name = serialTitleNodeArr![index].content!
                let serialDetailUrl: String = item.content!
                serial.detailUrl = Tool.checkUrl(urlStr: serialDetailUrl, domainUrlStr: baseUrl)
                model.serialArr.append(serial)
            }
        }
        videoModel.circuitArr = [model]
        videoModel.serialNum = 1
        //        推荐视频
        let titleXpath = "//ul[contains(@class,'myui-vodlist__bd')][position()<4]/li/div/a/@title"
        let urlXpath = "//ul[contains(@class,'myui-vodlist__bd')][position()<4]/li/div/a/@href"
        let imgXpath = "//ul[contains(@class,'myui-vodlist__bd')][position()<4]/li/div/a/@style"
        let titleNodeArr = jiDoc?.xPath(titleXpath)
        let urlNodeArr = jiDoc?.xPath(urlXpath)
        let imgNodeArr = jiDoc?.xPath(imgXpath)
        if titleNodeArr!.count > 0 {
            for (index,item) in imgNodeArr!.enumerated() {
                var model = VideoModel.init()
                model.name = titleNodeArr![index].content!
                var picUrl: String = item.content!
                picUrl = picUrl.replacingOccurrences(of: " ", with: "")
                picUrl = picUrl.replacingOccurrences(of: ";", with: "")
                picUrl = picUrl.replacingOccurrences(of: "background:url(", with: "")
                picUrl = picUrl.replacingOccurrences(of: "'", with: "")
                picUrl = picUrl.replacingOccurrences(of: ")", with: "")
                model.picUrl = Tool.checkUrl(urlStr: picUrl, domainUrlStr: baseUrl)
                let recommandUrlStr: String = urlNodeArr![index].content!
                model.detailUrl = Tool.checkUrl(urlStr: recommandUrlStr, domainUrlStr: baseUrl)
                model.webType = websiteType.lawyering.rawValue
                model.num = ""
                model.type = 3
                videoModel.videoArr.append(model)
            }
        }
        return (result: true, model: videoModel)
    }
    
    override func getVideoPlayerDetail(urlStr: String) -> (result: Bool, model: VideoModel) {
        let jiDoc = Ji(htmlURL: URL.init(string: urlStr)!)
        if jiDoc == nil {
            return (result: false, model: VideoModel.init())
        } else {
            let baseUrl = Tool.getRegularData(regularExpress: "((http://)|(https://))[^\\.]*\\.(?<domain>[^/|?]*)", content: urlStr)[0]
            var videoModel = VideoModel.init()
            videoModel.videoArr = []
            videoModel.serialArr = []
            // 播放地址
            let jsXpath = "//div[@class='myui-player__video embed-responsive clearfix']/script[1]"
            let jxNodeArr = jiDoc?.xPath(jsXpath)
            if jxNodeArr!.count>0{
                var jsItem = jxNodeArr![0].content!
                jsItem = jsItem.replacingOccurrences(of: "var player_aaaa=", with: "")
                let jsonData = jsItem.data(using: .utf8)!
                let js = try! JSONSerialization.jsonObject(with: jsonData)
                if let dic:Dictionary<String,Any> = js as? Dictionary<String, Any> {
                    let videoUrl = dic["url"] as! String
                    videoModel.videoUrl = videoUrl.removingPercentEncoding!
                }
            }else{
                return (result: true, model: videoModel)
            }
            // 获取线路
            let model = CircuitModel.init()
            model.name = "默认线路"
            let serialPathXpath = "//ul[@id='playlist']/li/a/@href"
            let serialtitleXpath = "//ul[@id='playlist']/li/a"
            let serialUrlNodeArr = jiDoc?.xPath(serialPathXpath)
            let serialTitleNodeArr = jiDoc?.xPath(serialtitleXpath)
            if serialUrlNodeArr!.count > 0 {
                for (index, item) in serialUrlNodeArr!.enumerated() {
                    let serial = SerialModel.init()
                    serial.name = serialTitleNodeArr![index].content!
                    let serialDetailUrl: String = item.content!
                    serial.detailUrl = Tool.checkUrl(urlStr: serialDetailUrl, domainUrlStr: baseUrl)
                    model.serialArr.append(serial)
                }
            }
            videoModel.circuitArr = [model]
            // 获取推荐视频
            let recommendtitleXpath = "//ul[contains(@class,'myui-vodlist__bd')][position()<4 and position()>1]/li/div/a/@title"
            let recommendurlXpath = "//ul[contains(@class,'myui-vodlist__bd')][position()<4 and position()>1]/li/div/a/@href"
            let recommendimgXpath = "//ul[contains(@class,'myui-vodlist__bd')][position()<4 and position()>1]/li/div/a/@style"
            let recommendTitleNodeArr = jiDoc?.xPath(recommendtitleXpath)
            let recommendUrlNodeArr = jiDoc?.xPath(recommendurlXpath)
            let recommendImgNodeArr = jiDoc?.xPath(recommendimgXpath)
            if recommendTitleNodeArr!.count > 0 {
                for (index,item) in recommendImgNodeArr!.enumerated() {
                    var model = VideoModel.init()
                    model.name = recommendTitleNodeArr![index].content!
                    var picUrl: String = item.content!
                    picUrl = picUrl.replacingOccurrences(of: " ", with: "")
                    picUrl = picUrl.replacingOccurrences(of: ";", with: "")
                    picUrl = picUrl.replacingOccurrences(of: "background:url(", with: "")
                    picUrl = picUrl.replacingOccurrences(of: "'", with: "")
                    picUrl = picUrl.replacingOccurrences(of: ")", with: "")
                    model.picUrl = Tool.checkUrl(urlStr: picUrl, domainUrlStr: baseUrl)
                    let recommandUrlStr: String = recommendUrlNodeArr![index].content!
                    model.detailUrl = Tool.checkUrl(urlStr: recommandUrlStr, domainUrlStr: baseUrl)
                    model.num = ""
                    model.type = 3
                    model.webType = websiteType.lawyering.rawValue
                    videoModel.videoArr.append(model)
                }
            }
            videoModel.videoUrl = videoModel.videoUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
            return (result: true, model: videoModel)
        }
    }
    
    override func getSearchData(pageNum: Int, keyword: String) -> [ListModel] {
        let urlStr = webUrlStr + "vodsearch/\(keyword)----------\(pageNum)---.html"
        let listModel = ListModel.init()
        listModel.title = "搜索关键字:" + keyword
        listModel.more = false
        listModel.list = []
        let baseUrl = Tool.getRegularData(regularExpress: "((http://)|(https://))[^\\.]*\\.(?<domain>[^/|?]*)", content: urlStr)[0]
        let newUrlStr = urlStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        let jiDoc = Ji(htmlURL: URL.init(string: newUrlStr)!)
        if jiDoc == nil {
            return []
        }
        let titleXpath = "//ul[@id='searchList']/li/div/a/@title"
        let urlXpath = "//ul[@id='searchList']/li/div/a/@href"
        let imgXpath = "//ul[@id='searchList']/li/div/a/@data-original"
        let titleNodeArr = jiDoc?.xPath(titleXpath)
        let detailNodeArr = jiDoc?.xPath(urlXpath)
        let imgNodeArr = jiDoc?.xPath(imgXpath)
        for (index, _) in titleNodeArr!.enumerated() {
            var videoModel = VideoModel.init()
            videoModel.name = titleNodeArr![index].content!
            videoModel.detailUrl = Tool.checkUrl(urlStr: detailNodeArr![index].content!, domainUrlStr: baseUrl)
            var picUrl: String = imgNodeArr![index].content!
            videoModel.picUrl = Tool.checkUrl(urlStr: picUrl, domainUrlStr: baseUrl)
            videoModel.type = 3
            videoModel.webType = websiteType.lawyering.rawValue
            listModel.list.append(videoModel)
        }
        return [listModel]
    }
}
